using System;
using System.Text.RegularExpressions;

namespace Tryit.Models
{
    public class ElementComposition
    {
        public string Name { get; set; }
        public double ActualValue { get; set; }
        public string TechnicalRequirement { get; set; }
        public string InternalRequirement { get; set; }
        public string Status { get; set; }

        public static bool ValidateRequirementFormat(string requirement)
        {
            if (string.IsNullOrWhiteSpace(requirement))
                return true;

            // 移除所有空格
            requirement = requirement.Replace(" ", "");

            // 定义正则表达式模式
            string pattern = @"^(≤|≥|＜|＞)?\d+(\.\d+)?(~(≤|≥|＜|＞)?\d+(\.\d+)?)?$";

            // 检查格式
            if (!Regex.IsMatch(requirement, pattern))
                return false;

            // 检查范围格式
            if (requirement.Contains("~"))
            {
                var parts = requirement.Split('~');
                if (parts.Length != 2)
                    return false;

                // 检查范围值是否合理
                if (!TryParseValue(parts[0], out double min) || !TryParseValue(parts[1], out double max))
                    return false;

                // 检查最小值是否小于最大值
                if (min >= max)
                    return false;
            }

            return true;
        }

        private static bool TryParseValue(string value, out double result)
        {
            // 移除所有比较符号
            value = value.Replace("≤", "").Replace("≥", "").Replace("＜", "").Replace("＞", "");
            return double.TryParse(value, out result);
        }

        public void ValidateStatus()
        {
            if (string.IsNullOrEmpty(TechnicalRequirement) || string.IsNullOrEmpty(InternalRequirement))
            {
                Status = "未知";
                return;
            }

            try
            {
                // 解析技术要求范围
                var techRange = ParseRange(TechnicalRequirement);
                // 解析内控要求范围
                var internalRange = ParseRange(InternalRequirement);

                // 检查是否在范围内
                if (ActualValue >= internalRange.Min && ActualValue <= internalRange.Max)
                {
                    Status = "正常";
                }
                else if (ActualValue >= techRange.Min && ActualValue <= techRange.Max)
                {
                    Status = "警告";
                }
                else
                {
                    Status = "异常";
                }
            }
            catch
            {
                Status = "未知";
            }
        }

        private (double Min, double Max) ParseRange(string range)
        {
            if (string.IsNullOrEmpty(range)) return (0, 0);

            // 移除所有空格
            range = range.Replace(" ", "");

            // 处理范围格式
            if (range.Contains("~"))
            {
                var parts = range.Split('~');
                if (parts.Length == 2)
                {
                    return (ParseValue(parts[0]), ParseValue(parts[1]));
                }
            }
            else if (range.Contains("≤"))
            {
                var value = ParseValue(range.Replace("≤", ""));
                return (0, value);
            }
            else if (range.Contains("≥"))
            {
                var value = ParseValue(range.Replace("≥", ""));
                return (value, double.MaxValue);
            }
            else if (range.Contains("＜"))
            {
                var value = ParseValue(range.Replace("＜", ""));
                return (0, value);
            }
            else if (range.Contains("＞"))
            {
                var value = ParseValue(range.Replace("＞", ""));
                return (value, double.MaxValue);
            }

            throw new FormatException("无法解析范围格式");
        }

        private double ParseValue(string value)
        {
            if (double.TryParse(value, out double result))
                return result;
            throw new FormatException($"无法解析数值：{value}");
        }
    }
} 